//封装分类数据业务相关的代码
import { onMounted, ref } from 'vue'
import { getCategoryAPI } from '@/apis/category'
import { onBeforeRouteUpdate } from 'vue-router'
import { useRoute } from 'vue-router'

export function useCategory(){
  const categoryData = ref({})
  const route = useRoute()
  const getCategory = async (id = route.params.id) => {
    const res = await getCategoryAPI(route.params.id)
    categoryData.value = res.result
  }
  onMounted(() => {
    getCategory()
  })

  //目标：路由参数发生变化时，可以把分类数据接口重新发送
  onBeforeRouteUpdate((to) => {
    console.log('路由变化了')
    //
    console.log(to)
    getCategory(to.params.id)
  })

  return {
    categoryData,
  }
}
